package question3;

import java.util.ArrayList;
import java.util.List;

public class GoodsList {
    private static List<Goods> goodsList = new ArrayList<Goods>();
    static {
        Goods g1 = new Goods(1,"电风扇",124.23);
        Goods g2 = new Goods(2,"洗衣机",4500.0);
        Goods g3 = new Goods(3,"电视机",8800.9);
        Goods g4 = new Goods(4,"冰箱机",5800.88);
        Goods g5 = new Goods(5,"空调机",4456.0);
        goodsList.clear();
        goodsList.add(g1);
        goodsList.add(g2);
        goodsList.add(g3);
        goodsList.add(g4);
        goodsList.add(g5);
    }
    public void showMenu(){
        System.out.println("*******欢迎来到商品管理系统*******");
        System.out.println("编号\t\t商品名称\t\t\t价格");
        for(Goods goods :goodsList){
            System.out.println(goods.getId()+"\t\t"+goods.getName()+"\t\t\t"+goods.getPrice());
        }
        System.out.println("*********************************");
    }
    public String buyGoods(int choose,int amount){
        double sumPrice = 0;
        for(int i = 0 ; i < goodsList.size(); i++){
            if(goodsList.get(i).getId() == choose ){
                sumPrice = goodsList.get(i).getPrice() * amount;
                break;
            }
        }
        String strSumPrice = String.valueOf(sumPrice);
        StringBuffer str = new StringBuffer(strSumPrice);
        int index = str.indexOf(".");
        for(int i = index - 3 ; i >0;i -= 3){
        str.insert(i,",");
        }
        return str.toString();
    }
}
